<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>


    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        .box {
            width: 480px;
            height: 400px;
            margin: 0 auto;
            margin-top: 50px;
            display: flex;
            flex-wrap: wrap;
            justify-content: space-around;
            background-color: #26cafc;

        }

        .box1 {
            width: 480px;
            height: 60px;
            margin: 10px 0;
            display: flex;
            justify-content: space-around;
            /* align-content: flex-start; */
            /* flex-wrap: wrap; */
        }

        .box2 {
            width: 130px;
            height: 60px;
            border: 1px solid #000;

            text-align: center;
            line-height: 55px;
        }



        button {
            position: absolute;
            /* margin-top: 20px; */
            left: 50%;
            transform: translateX(-50%);
        }
    </style>
</head>

<body>
    <script>
        
            var arr = [{ name: 'Jack', age: 18, gender: '男' }, { name: 'Rose', age: 20, gender: '女' }, { name: 'Top', age: 22, gender: '男' }]
            function fn() {
                document.write('<div class="box">');
                for (var i = 0; i < arr.length; i++) {
                    document.write('<div class="box1">');
                        // if(i%2==0){
                        //     var box33 = document.querySelector(".box1");
                        // box33.style.backgroundColor= "blue";
                        // }else{
                        //     var box33 = document.querySelector(".box1");
                        // box33.style.backgroundColor= "red";
                        // }
                    for (var key in arr[i]) {
                        document.write('<div class="box2">');
                        document.write(arr[i][key]);
                        document.write("</div>");
                    }
                    document.write("</div>");
                }
                document.write("</div>");
            }
            fn();
            
        var box33 = document.querySelectorAll(".box1")  // 你生成了3个.box1，要用querySelectorAll，然后再进循环
        box33.forEach((item,key)=>{
            if(key%2==0){
                item.style.background = "red"
            }
        })
            // var box33 = document.querySelector(".box1");
            // box33.setAttribute("style","background-color:red");
            // // box33.style['background-color'] = "blue";
            // box33.style.backgroundColor= "blue";

    </script>


</body>

</html>